﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;

namespace MazeWars
{
    public partial class SaveForm : Form
    {
        Tool tool;
        Trunk temp;
        List<Trunk> tempList;
 
        public SaveForm()
        {
            InitializeComponent();

            saveFileDialog1.DefaultExt = ".xml";
            saveFileDialog1.Filter = "XML File (*.xml)|*.xml|All files (*.*)|*.*";
            saveFileDialog1.AddExtension = true;
            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.InitialDirectory = @"C:\";

            openFileDialog1.DefaultExt = ".xml";
            openFileDialog1.Filter = "XML File (*.xml)|*.xml|All files (*.*)|*.*";
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.InitialDirectory = @"C:\";
        }

        public void setTool(Tool t)
        {
            tool = t;
        }

        //The Close Button
        private void button2_Click(object sender, EventArgs e)
        {
            Hide();
        }

        //The Remove Button
        private void button5_Click(object sender, EventArgs e)
        {
            listBox1.Items.Remove(listBox1.SelectedItem);
            textBox1.Text = "";
            textBox2.Text = "";
            tool.reset();
        }

        //The Add Button
        private void button4_Click(object sender, EventArgs e)
        {
            bool nodeFound = false;
            if (textBox1.TextLength != 0 && textBox2.TextLength != 0)
            {
                foreach (Tile t in tool.MyTrunk.TrunkTiles)
                {
                    if (t.Type % 2 == 1)
                        nodeFound = true;
                }
                if (nodeFound)
                {
                    tool.MyTrunk.Name = textBox1.Text;
                    tool.MyTrunk.SpellID = System.Convert.ToInt32(textBox2.Text);
                    listBox1.Items.Add(tool.MyTrunk);
                    tool.reset();
                    textBox1.Text = "";
                    textBox2.Text = "";
                }
                else
                {
                    textBox1.Text = "Add node to trunk";
                }
            }            
        }

        //Clicking an item in the list.
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                temp = listBox1.SelectedItem as Trunk;
                if (temp != null)
                {
                    tool.MyTrunk = temp;
                    tool.X = tool.MyTrunk.TrunkTiles[tool.MyTrunk.TrunkTiles.Count - 1].X;
                    tool.Y = tool.MyTrunk.TrunkTiles[tool.MyTrunk.TrunkTiles.Count - 1].Y;
                    textBox1.Text = tool.MyTrunk.Name;
                    textBox2.Text = tool.MyTrunk.SpellID.ToString();
                }
                else
                {
                    textBox1.Text = "Cast failed";
                }
            }
            else
            {
                textBox1.Text = "";
                textBox2.Text = "";
            }
        }

        //File->Save
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tempList = new List<Trunk>();
            foreach (object o in listBox1.Items)
            {
                tempList.Add((Trunk)o);
            }
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                FileStream file = File.Open(saveFileDialog1.FileName, FileMode.Create);

                XmlSerializer xml = new XmlSerializer(typeof(List<Trunk>));
                xml.Serialize(file, tempList);

                file.Close();
                file.Dispose();
            }

            saveFileDialog1.Dispose();
        }

        //File->Load
        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tempList = new List<Trunk>();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                FileStream file = File.Open(openFileDialog1.FileName, FileMode.Open);
                XmlSerializer xml = new XmlSerializer(typeof(List<Trunk>));
                tempList = (List<Trunk>)xml.Deserialize(file);
                file.Close();
                file.Dispose();

                listBox1.Items.Clear();

                foreach (Trunk t in tempList)
                {
                    listBox1.Items.Add(t);
                }
            }

            openFileDialog1.Dispose();
        }
    }//End of class SaveForm
}
